package com.lczyfz.campusinfo.modules.frontuser.web;

/**
 * Created by yt on 2017/10/28.
 */

import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.frontuser.entity.CampusinfoFrontUser;
import com.lczyfz.campusinfo.modules.frontuser.utils.FrontUserUtils;
import com.lczyfz.campusinfo.modules.sys.security.FormAuthenticationFilter;
import com.lczyfz.campusinfo.modules.sys.security.SystemAuthorizingRealm;
import com.lczyfz.campusinfo.modules.sys.security.UsernamePasswordToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 前端用户管理Controller 接口类
 *
 * @author yt
 * @version 2017-10-26
 */
@Controller
public class CampusinfoFrontUserApiController extends BaseController {

    /**
     * 登录接口类 需传入以下三个类型参数 提交方式为 POST
     *
     * @param username 用户名
     * @param password 密码
     * @param type     用户类型 (用户类型 type 为 1 前端用户)
     */
    @ResponseBody
    @RequestMapping(value = "/api/loginFront", method = RequestMethod.POST)
    public Map<String, Object> loginFail(HttpServletRequest request, HttpServletResponse response, CampusinfoFrontUser campusinfoFrontUser) {

        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();

        SystemAuthorizingRealm.Principal principal = FrontUserUtils.getPrincipal();
        if (principal != null) {
            map.put("message", "已经登录");
            return map;
        }

        String username = WebUtils.getCleanParam(request, FormAuthenticationFilter.DEFAULT_USERNAME_PARAM);

        System.out.println(" 33333 ========================================================" + username);

        String password = WebUtils.getCleanParam(request, FormAuthenticationFilter.DEFAULT_PASSWORD_PARAM);

        System.out.println(" 33333 ========================================================" + password);

        String type = request.getParameter("type").toString();

        System.out.println(" 33333 ========================================================" + type);

        Subject currentUser = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(username, password, type);

        currentUser.login(token);

        return map;
    }
}
